package com.zoe.onelink.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.zoe.onelink.mybatis.properties.OnelinkMybatisPluginProperties;
import com.zoe.onelink.mybatis.provider.OperatorProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;


/**
 * <p>标题: 通用字段填充器</p>
 * <p>描述: 对创建人,创建时间等通用自动进行自动填充 </p>
 * <p>版权: Copyright (c) 2020</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2020-10-13
 */
@Slf4j
public class CommonMetaHandler implements MetaObjectHandler {

    private final OnelinkMybatisPluginProperties.MetaField metaField;
    private final OperatorProvider operatorProvider;

    public CommonMetaHandler(OnelinkMybatisPluginProperties onelinkMybatisProperties, OperatorProvider operatorProvider) {
        this.metaField = onelinkMybatisProperties.getMetaField();
        this.operatorProvider = operatorProvider;
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setCreatorCode(metaObject);
        this.setModifierCode(metaObject);
        this.setCreateTime(metaObject);
        this.setUpdateTime(metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setModifierCode(metaObject);
        this.setUpdateTime(metaObject);

    }

    /**
     * 设置修改人编码
     *
     * @param metaObject 元数据
     */
    private void setModifierCode(MetaObject metaObject) {
        String operator = this.operatorProvider.getOperator();
        this.setFieldData(this.metaField.getModifierCode(), operator, metaObject);
    }

    /**
     * 设置创建人编码
     *
     * @param metaObject 元数据
     */
    private void setCreatorCode(MetaObject metaObject) {
        String operator = this.operatorProvider.getOperator();
        this.setFieldData(this.metaField.getCreatorCode(), operator, metaObject);
    }


    /**
     * 设置更新时间
     *
     * @param metaObject 元数据
     */
    private void setUpdateTime(MetaObject metaObject) {
        this.setFieldData(this.metaField.getModifiedTime(), new Date(), metaObject);
    }

    /**
     * 设置创建时间
     *
     * @param metaObject 元数据
     */
    private void setCreateTime(MetaObject metaObject) {
        this.setFieldData(this.metaField.getCreatedTime(), new Date(), metaObject);
    }


    /**
     * 设置字段数据
     *
     * @param fieldName  字段名
     * @param fieldVal   字段值
     * @param metaObject 元数据
     */
    private void setFieldData(String fieldName, Object fieldVal, MetaObject metaObject) {
        if (!this.hasField(fieldName, metaObject)) {
            return;
        }
        this.setFieldValByName(fieldName, fieldVal, metaObject);
    }

    /**
     * 是否有该字段
     *
     * @param fieldName  字段名称
     * @param metaObject 元数据
     * @return 有 = true , 没有 = false
     */
    private boolean hasField(String fieldName, MetaObject metaObject) {
        return metaObject.hasGetter(fieldName) || metaObject.hasGetter(Constants.ENTITY_DOT + fieldName);
    }



}
